home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / CODECS.ZIP / codecs / francais / compress.txt < prev    next >
Encoding:
Text File  |  1995-10-13  |  48.8 KB  |  1,094 lines

  1. +=====================================================+
  2. | Introduction aux systemes de compression sans perte |
  3. |   Description des fichiers source et des methodes   |
  4. +-----------------------------------------------------+
  5. | De David Bourgin (E-mail: dbourgin@ufrima.imag.fr)  |
  6. | Date: 12/10/95                         VERSION: 1.5 |
  7. +=====================================================+
  8.  
  9.               ------ ATTENTION ------
  10. Ce fichier doit etre donne dans un paquetage contenant les fichiers suivants
  11. dans deux repertoires:
  12. * Fichiers francais dans le repertoire codecs.dir/francais
  13. lisezmoi,compress.txt,codrle1.c,codrle2.c,codrle3.c,codrle4.c,codhuff.c,codlzw.c,
  14. dcodrle1.c,dcodrle2.c,dcodrle3.c,dcodrle4.c,dcodhuff.c,dcodlzw.c
  15. * Fichiers anglais dans le repertoire codecs.dir/anglais
  16. readme,compress.txt,codrle1.c,codrle2.c,codrle3.c,codrle4.c,codhuff.c,codlzw.c,
  17. dcodrle1.c,dcodrle2.c,dcodrle3.c,dcodrle4.c,dcodhuff.c,dcodlzw.c
  18.  
  19. S'il vous plait, lisez le fichier 'lisezmoi' pour obtenir des infos sur les
  20. copyrights et le contenu des fichiers.
  21.  
  22. Faute de temps, je n'ai pas pu traduire le reste de ce fichier. Aussitot que
  23. je le pourrais, je le ferai. Mille excuses pour les adorateurs de notre langue
  24. bien-aimee! ;-)
  25.               ---------------------
  26.  
  27. There are several means to compress data. Here, we are only going to deal with
  28. the losslessy schemes. These schemes are also called non-destructive because
  29. you always recover the initial data you had, and this, as soon as you need them.
  30. With losslessy schemes, you won't never lose any informations (except perhaps
  31. when you store or transmit your data but this is another problem...).
  32.  
  33. In this introduction, we are going to see:
  34. - The RLE scheme (with different possible algorithms)
  35. - The Huffman schemes (dynamical scheme)
  36. - And the LZW scheme
  37.  
  38. For the novice, a compresser is a program able to read several data (e.g. bytes)
  39. in input and to write several data in output. The data you obtain from the
  40. output (also called compressed data) will - of course - take less space than
  41. the the input data. This is true in most of cases, if the compresser works
  42. and if the type of the data is correct to be compressed with the given scheme.
  43. The codec (coder-decoder) enables you to save space on your hard disk and/or
  44. to save the communication costs because you always store/transmit the compressed
  45. data. You'll use the decompresser as soon as you need to recover your initial
  46. useful data. Note that the compressed data are useless if you have not
  47. the decoder...
  48.  
  49. You are doubtless asking "How can I reduce the data size without losing some
  50. informations?". It's easy to answer to this question. I'll only take an example.
  51. I'm sure you have heard about the morse. This system established in the 19th
  52. century use a scheme very close to the huffman one. In the morse you encode
  53. the letters to transmit with two kinds of signs. If you encode these two sign
  54. possibilities in one bit, the symbol 'e' is transmitted in a single bit and
  55. the symbols 'y' and 'z' need four bits. Look at the symbols in the text you are
  56. reading, you'll fast understand the compression ratio...
  57.  
  58. From there I explain into two parts:
  59. - How to change the source codes
  60. - What are RLE1, RLE2, RLE3, Huffman, and LZW encoding/decoding
  61.  
  62. ********** FIRST PART: Source modifications you can do **********
  63.  
  64. Important: The source codes associated to the algorithms I present are
  65. completely adaptative on what you need to compress. They all use basical
  66. macros on the top of the file. Usually the macros to change are:
  67.  
  68. - beginning_of_data
  69. - end_of_data
  70. - read_byte
  71. - read_block
  72. - write_byte
  73. - write_block
  74.  
  75. These allow the programmer to modify only a little part of the header
  76. of the source codes in order to compress as well memory as files.
  77.  
  78. beginning_of_data(): Macro used to set the program so that the next read_byte()
  79. call will read the first byte to compress.
  80. end_of_data(): Returns a boolean to know whether there is no more bytes to read
  81. from the input stream. Return 0 if there is no more byte to compress, another
  82. non-zero value otherwise.
  83. read_byte(): Returns a byte read from the input stream if available.
  84. write_byte(x): Writes the byte 'x' to the output stream.
  85. read_block(...) and write_block(...): Same use as read_byte and write_byte(x)
  86. but these macros work on blocks of bytes and not only on a single byte.
  87.  
  88. If you want to compress *from* the memory, before entering in a xxxcoding
  89. procedure ('xxx' is the actual extension to replace with a given codec), you
  90. have to add a pointer set up to the beginning of the zone to compress. Note
  91. that the following pointer 'source_memory_base' is not to add, it is just given
  92. here to specify a name to the address of the memory zone you are going to
  93. encode or decode. That is the same about source_memory_end which can be either
  94. a pointer to create or an existing pointer.
  95.  
  96. unsigned char *source_memory_base, /* Base of the source memory */
  97.           *source_memory_end,  /* Last address to read.
  98. source_memory_end=source_memory_base+source_zone_length-1 */
  99.           *source_ptr;         /* Used in the xxxcoding procedure */
  100. void pre_start()
  101. { source_ptr=source_memory_base;
  102.   xxxcoding();
  103. }
  104.  
  105. end_of_data() and read_byte() are also to modify to compress *from* memory:
  106.  
  107. #define end_of_data()  (source_ptr>source_memory_end)
  108. #define read_byte()  (*(source_ptr++))
  109.  
  110. If you want to compress *to* memory, before entering in a xxxcoding procedure
  111. ('xxx' is the actual extension to replace with a given codec), you have to add
  112. a pointer. Note that the pointer 'dest_memory_base' is not to add, it is just
  113. given there to specify the address of the destination memory zone you are
  114. going to encode or decode.
  115.  
  116. unsigned char *dest_memory_base, /* Base of the destination memory */
  117.           *dest_ptr;         /* Used in the xxxcoding procedure */
  118. void pre_start()
  119. { dest_ptr=dest_memory_base;
  120.   xxxcoding();
  121. }
  122.  
  123. Of course, you can combine both from and to memory in the pre_start() procedure.
  124. The files dest_file and source_file handled in the main() function are
  125. to remove...
  126.  
  127. void pre_start()
  128. { source_ptr=source_memory_base;
  129.   dest_ptr=dest_memory_base;
  130.   xxxcoding();
  131. }
  132.  
  133. In fact, to write to memory, the problem is in the write_byte(x) procedure.
  134. This problem exists because your destination zone can either be a static
  135. zone or a dynamically allocated zone. In the two cases, you have to check
  136. if there is no overflow, especially if the coder is not efficient and must
  137. produce more bytes than you reserved in memory.
  138.  
  139. In the first case, with a *static* zone, write_byte(x) macro should look like
  140. that:
  141.  
  142. unsigned long int dest_zone_length,
  143.           current_size;
  144.  
  145. #define write_byte(x)  { if (current_size==dest_zone_length) \
  146.                 exit(1); \
  147.              dest_ptr[current_size++]=(unsigned char)(x); \
  148.                }
  149.  
  150. In the static version, the pre_start() procedure is to modify as following:
  151.  
  152. void pre_start()
  153. { source_ptr=source_memory_base;
  154.   dest_ptr=dest_memory_base;
  155.   dest_zone_length=...; /* Set up to the actual destination zone length */
  156.   current_size=0; /* Number of written bytes */
  157.   xxxcoding();
  158. }
  159. Otherwise, dest_ptr is a zone created by the malloc instruction and you can try
  160. to resize the allocated zone with the realloc instruction. Note that I increment
  161. the zone one kilo-bytes by one kylo-bytes. You have to add two other variables:
  162.  
  163. unsigned long int dest_zone_length,
  164.           current_size;
  165.  
  166. #define write_byte(x)  { if (current_size==dest_zone_length) \
  167.                 { dest_zone_length += 1024; \
  168.                   if ((dest_ptr=(unsigned char *)realloc(dest_ptr,dest_zone_length*sizeof(unsigned char)))==NULL) \
  169.                  exit(1); /* You can't compress in memory \
  170.                            => I exit but *you* can make a routine to swap on disk */ \
  171.                 } \
  172.              dest_ptr[current_size++]=(unsigned char)(x); \
  173.                }
  174.  
  175. With the dynamically allocated version, change the pre_start() routine as following:
  176.  
  177. void pre_start()
  178. { source_ptr=source_memory_base;
  179.   dest_ptr=dest_memory_base;
  180.   dest_zone_length=1024;
  181.   if ((dest_ptr=(unsigned char *)malloc(dest_zone_length*sizeof(unsigned char)))==NULL)
  182.      exit(1); /* You need at least 1 kb in the dynamical memory ! */
  183.   current_size=0; /* Number of written bytes */
  184.   xxxcoding();
  185.   /* Handle the bytes in dest_ptr but don't forget to free these bytes with:
  186.      free(dest_ptr);
  187.   */
  188. }
  189.  
  190. The previously given macros work as:
  191.  
  192. void demo()       /* The file opening, closing and variables
  193.              must be set up by the calling procedure */
  194. { unsigned char byte;
  195.           /* And not 'char byte' (!) */
  196.   while (!end_of_data())
  197.     { byte=read_byte();
  198.       printf("Byte read=%c\n",byte);
  199.     }
  200. }
  201.  
  202. You must not change the rest of the program unless you're really sure and
  203. really need to do it!
  204.  
  205. ********** SECOND PART: Encoding/Decoding explainations **********
  206.  
  207. +==========================================================+
  208. |                     The RLE encoding                     |
  209. +==========================================================+
  210.  
  211. RLE is an acronym that stands for Run Length Encoding. You may encounter it
  212. as an other acronym: RLC, Run Length Coding.
  213.  
  214. The idea in this scheme is to recode your data with regard to the repetition
  215. frames. A frame is one or more bytes that occurr one or several times.
  216.  
  217. There are several means to encode occurrences. So, you'll have several codecs.
  218. For example, you may have a sequence such as:
  219. 0,0,0,0,0,0,255,255,255,2,3,4,2,3,4,5,8,11
  220.  
  221. Some codecs will only deal with the repetitions of '0' and '255' but some other
  222. will deal with the repetitions of '0', '255', and '2,3,4'.
  223.  
  224. You have to keep in your mind something important based on this example. A codec
  225. won't work on all the data you will try to compress. So, in case of non
  226. existence of sequence repetitions, the codecs based on RLE schemes must not
  227. display a message to say: "Bye bye". Actually, they will try to encode these
  228. non repeted data with a value that says "Sorry, I only make a copy of the inital
  229. input". Of course, a copy of the input data with an header in front of this copy
  230. will make a biggest output data but if you consider the whole data to compress,
  231. the encoding of repeated frames will take less space than the encoding
  232. of non-repeated frames.
  233.  
  234. All of the algorithms with the name of RLE have the following look with three
  235. or four values:
  236. - Value saying if there's a repetition
  237. - Value saying how many repetitions (or non repetition)
  238. - Value of the length of the frame (useless if you just encode frame
  239. with one byte as maximum length)
  240. - Value of the frame to repeat (or not)
  241.  
  242. I gave four algorithms to explain what I say.
  243.  
  244. *** First RLE scheme ***
  245.  
  246. The first scheme is the simpliest I know, and looks like the one used in MAC
  247. system (MacPackBit) and some image file formats such as Targa, PCX, TIFF, ...
  248.  
  249. Here, all compressed blocks begin with a byte, named header, which description
  250. is:
  251.  
  252. Bits   7 6 5 4 3 2 1 0
  253. Header X X X X X X X X
  254.  
  255. Bits 7: Compression status (1=Compression applied)
  256.      0 to 6: Number of bytes to handle
  257.  
  258. So, if the bit 7 is set up to 0, the 0 to 6 bits give the number of bytes
  259. that follow (minus 1, to gain more over compress) and that were not compressed
  260. (native bytes). If the bit 7 is set up to 1, the same 0 to 6 bits give
  261. the number of repetition (minus 2) of the following byte.
  262.  
  263. As you see, this method only handle frame with one byte.
  264.  
  265. Additional note: You have 'minus 1' for non-repeated frames because you must
  266. have at least one byte to compress and 'minus 2' for repeated frames because the
  267. repetition must be 2, at least.
  268.  
  269. Compression scheme:
  270.  
  271.           First byte=Next
  272.             /\
  273.            /  \
  274. Count the byte         Count the occurrence of NON identical
  275. occurrences            bytes (maximum 128 times)
  276. (maximum 129 times)    and store them in an array
  277.     |                        |
  278.     |                        |
  279.   1 bit '1'                 1 bit '0'
  280. + 7 bits giving           + 7 bits giving
  281.   the number (-2)           the number (-1)
  282.   of repetitions            of non repetition
  283. + repeated byte           + n non repeated bytes
  284.     |                        |
  285.  1xxxxxxx,yyyyyyyy        0xxxxxxx,n bytes
  286. [-----------------]      [----------------]
  287.  
  288. Example:
  289.  
  290. Sequence of bytes to encode | Coded values | Differences with compression
  291.                 |              |         (unit: byte)
  292. -------------------------------------------------------------------------
  293.        255,15,              |  1,255,15,   |            -1
  294.        255,255,             |    128,255,  |             0
  295.     15,15,              |    128,15,   |             0
  296.      255,255,255,           |   129,255,   |            +1
  297.        15,15,15,            |    129,15,   |            +1
  298.    255,255,255,255,         |   130,255,   |            +2
  299.      15,15,15,15            |    130,15    |            +2
  300.  
  301. See codecs source codes: codrle1.c and dcodrle1.c
  302.  
  303. *** Second RLE scheme ***
  304.  
  305. In the second scheme of RLE compression you look for the less frequent byte
  306. in the source to compress and use it as an header for all compressed block.
  307.  
  308. In the best cases, the occurrence of this byte is zero in the data to compress.
  309.  
  310. Two possible schemes, firstly with handling frames with only one byte,
  311. secondly with handling frames with one byte *and* more. The first case is
  312. the subject of this current compression scheme, the second is the subject
  313. of next compression scheme.
  314.  
  315. For the frame of one byte, header byte is written in front of all repetition
  316. with at least 4 bytes. It is then followed by the repetition number minus 1 and
  317. the repeated byte.
  318. Header byte, Occurrence number-1, repeated byte
  319.  
  320. If a byte don't repeat more than tree times, the three bytes are written without
  321. changes in the destination stream (no header nor length, nor repetition in front
  322. or after theses bytes).
  323.  
  324. An exception: If the header byte appears in the source one, two, three and up
  325. times, it'll be respectively encoded as following:
  326. - Header byte, 0
  327. - Header byte, 1
  328. - Header byte, 2
  329. - Header byte, Occurrence number-1, Header byte
  330.  
  331. Example, let's take the previous example. A non frequent byte is zero-ASCII
  332. because it never appears.
  333.  
  334. Sequence of bytes to encode | Coded values | Differences with compression
  335.                 |              |         (unit: byte)
  336. -------------------------------------------------------------------------
  337.        255,15,              |    255,15,   |             0
  338.        255,255,             |   255,255,   |             0
  339.     15,15,              |     15,15,   |             0
  340.      255,255,255,           | 255,255,255, |             0
  341.        15,15,15,            |   15,15,15,  |             0
  342.    255,255,255,255,         |   0,3,255,   |            -1
  343.      15,15,15,15            |    0,3,15    |            -1
  344.  
  345. If the header would appear, we would see:
  346.  
  347. Sequence of bytes to encode | Coded values | Differences with compression
  348.                 |              |         (unit: byte)
  349. -------------------------------------------------------------------------
  350.       0,                |      0,0,    |            +1
  351.      255,               |      255,    |             0
  352.      0,0,               |      0,1,    |             0
  353.      15,                |      15,     |             0
  354.     0,0,0,              |      0,2,    |            -1
  355.      255,               |      255,    |             0
  356.        0,0,0,0              |     0,3,0    |            -1
  357.  
  358. See codecs source codes: codrle2.c and dcodrle2.c
  359.  
  360. *** Third RLE scheme ***
  361.  
  362. It's the same idea as the second scheme but we can encode frames with
  363. more than one byte. So we have three cases:
  364.  
  365. - If it was the header byte, whatever is its occurrence, you encode it with:
  366. Header byte,0,number of occurrence-1
  367. - For frames which (repetition-1)*length>3, encode it as:
  368. Header byte, Number of frame repetition-1, frame length-1,bytes of frame
  369. - If no previous cases were detected, you write them as originally (no header,
  370. nor length, nor repetition in front or after theses bytes).
  371.  
  372. Example based on the previous examples:
  373.  
  374. Sequence of bytes to encode |   Coded values   | Differences with compression
  375.                 |                  |         (unit: byte)
  376. -----------------------------------------------------------------------------
  377.        255,15,          |      255,15,     |             0
  378.        255,255,         |     255,255,     |             0
  379.         15,15,          |       15,15,     |             0
  380.      255,255,255,       |   255,255,255,   |             0
  381.        15,15,15,        |     15,15,15,    |             0
  382.        255,255,255,255,     | 255,255,255,255, |             0
  383.      15,15,15,15,       |   15,15,15,15,   |             0
  384.       16,17,18,16,17,18,    |16,17,18,16,17,18,|             0
  385.      255,255,255,255,255,   |    0,4,0,255,    |            -1
  386.        15,15,15,15,15,      |     0,4,0,15,    |            -1
  387.  16,17,18,16,17,18,16,17,18,|  0,2,2,16,17,18, |            -3
  388.   26,27,28,29,26,27,28,29   |0,1,3,26,27,28,29 |            -1
  389.  
  390. If the header (value 0) would be met, we would see:
  391.  
  392. Sequence of bytes to encode | Coded values  | Differences with compression
  393.                 |               |         (unit: byte)
  394. --------------------------------------------------------------------------
  395.       0,                |     0,0,0,    |            +2
  396.      255,               |      255,     |             0
  397.      0,0,               |     0,0,1,    |            +1
  398.       15,               |       15,     |             0
  399.     0,0,0,              |     0,0,2,    |             0
  400.      255,               |      255,     |             0
  401.        0,0,0,0              |     0,0,3     |            -1
  402.  
  403. See codecs source codes: codrle3.c and dcodrle3.c
  404.  
  405. *** Fourth RLE scheme ***
  406.  
  407. This last RLE algorithm better handles repetitions of any kind (one byte
  408. and more) and non repetitions, including few non repetitions, and does not
  409. read the source by twice as RLE type 3.
  410.  
  411. Compression scheme is:
  412.  
  413.           First byte=Next byte?
  414.                /\
  415.               Yes /  \ No
  416.              /    \
  417.          1 bit '0'     1 bit '1'
  418.                /        \
  419.               /          \
  420.        Count the                    Motif of several
  421.        occurrences                  repeated  byte?
  422.        of 1 repeated                ( 65 bytes repeated
  423.        byte (maximum                257 times maxi)
  424.        16449 times)                           /\
  425.         /\                               /  \
  426.        /  \                             /    \
  427.       /    \                           /      \
  428.      /      \                         /        \
  429.   1 bit '0'       1 bit '1'        1 bit '0'          1 bit '1'
  430. + 6 bits        + 14 bits        + 6 bits of              |
  431. giving the      giving the       the length      Number of non repetition
  432. length (-2)     length (-66)     of the motif         (maximum 8224)
  433. of the          of the           + 8 bits of               /\
  434. repeated byte   repeated byte    the number (-2)     < 33 /  \ > 32
  435. + repeated byte + repeated byte  of repetition           /    \
  436.     |                |           + bytes of the   1 bit '0'       1 bit '1'
  437.     |                |           motif          + 5 bits of     + 13 bits
  438.     |                |               |          the numer (-1)  of the
  439.     |                |               |          of non          number (-33)
  440.     |                |               |          repetition      of repetition
  441.     |                |               |          + non           + non
  442.     |                |               |          repeated        repeated
  443.     |                |               |          bytes           bytes
  444.     |                |               |             |               |
  445.     |                |               |             |  111xxxxx,xxxxxxxx,n bytes
  446.     |                |               |             | [-------------------------]
  447.     |                |               |             |
  448.     |                |               |      110xxxxx,n bytes
  449.     |                |               |     [----------------]
  450.     |                |               |
  451.     |                |  10xxxxxx,yyyyyyyy,n bytes
  452.     |                | [-------------------------]
  453.     |                |
  454.     |   01xxxxxx,xxxxxxxx,1 byte
  455.     |  [------------------------]
  456.     |
  457.  00xxxxxx,1 byte
  458. [---------------]
  459.  
  460. Example, same as previously:
  461.  
  462. Sequence of bytes to encode |         Coded values          | Differences with compression
  463.                 |                               |         (unit: byte)
  464. ------------------------------------------------------------------------------------------
  465.     255,15,255,255,15,15    |11000101b,255,15,255,255,15,15 |             +1
  466.      255,255,255            |        00000001b,255,         |             -1
  467.        15,15,15             |         00000001b,15,         |             -1
  468.    255,255,255,255          |         00000010b,255,        |             -2
  469.      15,15,15,15            |          00000010b,15,        |             -2
  470.   16,17,18,16,17,18         |     10000001b,0,16,17,18,     |             -1
  471.  255,255,255,255,255        |        00000011b,255,         |             -3
  472.    15,15,15,15,15           |         00000011b,15,         |             -3
  473.  16,17,18,16,17,18,16,17,18 |      10000001b,16,17,18,      |             -4
  474.   26,27,28,29,26,27,28,29   |     10000010b,26,27,28,29     |             -2
  475.  
  476. +==========================================================+
  477. |                   The Huffman encoding                   |
  478. +==========================================================+
  479.  
  480. This method comes from the searcher who established the algorithm in 1952.
  481. This method allows both a dynamic and static statistic schemes. A statistic
  482. scheme works on the data occurrences. It is not as with RLE where you had
  483. a consideration of the current occurrence of a frame but rather a consideration
  484. of the global occurrences of each data in the input stream. In this last case,
  485. frames can be any kinds of sequences you want. On the other hand, Huffman
  486. static encoding appears in some compressers such as ARJ on PCs. This enforces
  487. the encoder to consider every statistic as the same for all the data you have.
  488. Of course, the results are not as good as if it were a dynamic encoding.
  489. The static encoding is faster than the dynamic encoding but the dynamic encoding
  490. will be adapted to the statistic of the bytes of the input stream and will
  491. of course become more efficient by producing shortest output.
  492.  
  493. The main idea in Huffman encoding is to re-code every byte with regard to its
  494. occurrence. The more frequent bytes in the data to compress will be encoded with
  495. less than 8 bits and the others could need 8 bits see even more to be encoded.
  496. You immediately see that the codes associated to the different bytes won't have
  497. identical size. The Huffman method will actually require that the binary codes
  498. have not a fixed size. We speak then about variable length codes.
  499.  
  500. The dynamical Huffman scheme needs the binary trees for the encoding. This
  501. enables you to obtain the best codes, adapted to the source data.
  502. The demonstration won't be given there. To help the neophyt, I will just explain
  503. what is a binary tree.
  504.  
  505. A binary tree is special fashion to represent the data. A binary tree is
  506. a structure with an associated value with two pointers. The term of binary has
  507. been given because of the presence of two pointers. Because of some conventions,
  508. one of the pointer is called left pointer and the second pointer is called right
  509. pointer. Here is a visual representation of a binary tree.
  510.  
  511.      Value
  512.       / \
  513.      /   \
  514.  Value    Value
  515.   / \      / \
  516. ... ...  ... ...
  517.  
  518. One problem with a binary encoding is a prefix problem. A prefix is the first
  519. part of the representation of a value, e.g. "h" and "he" are prefixes of "hello"
  520. but not "el". To understand the problem, let's code the letters "A", "B", "C",
  521. "D", and "E" respectively as 00b, 01b, 10b, 11b, and 100b. When you read
  522. the binary sequence 00100100b, you are unable to say if this comes from "ACBA"
  523. or "AEE". To avoid such situations, the codes must have a prefix property.
  524. And the letter "E" mustn't begin with the sequence of an other code. With "A",
  525. "B", "C", "D", and "E" respectively affected with 1b, 01b, 001b, 0001b, and
  526. 0000b, the sequence 1001011b will only be decoded as "ACBA".
  527.  
  528.  1      0
  529. <-  /\  ->
  530.    /  \
  531.  "A"  /\
  532.     "B" \
  533.     /\
  534.       "C" \
  535.       /\
  536.        "D"  "E"
  537.  
  538. As you see, with this tree, an encoding will have the prefix property
  539. if the bytes are at the end of each "branch" and you have no byte at the "node".
  540. You also see that if you try to reach a character by the right pointer you add
  541. a bit set to 0 and by the left pointer, you add a bit set to 1 to the current
  542. code. The previous *bad* encoding provide the following bad tree:
  543.  
  544.        /\
  545.       /  \
  546.      /    \
  547.     /\    /\
  548.    /  \ "B" "A"
  549.   /    \
  550. "D"  "C"\
  551.       /  \
  552.      "E"
  553.  
  554. You see here that the coder shouldn't put the "C" at a node...
  555.  
  556. As you see, the largest binary code are those with the longest distance
  557. from the top of the tree. Finally, the more frequent bytes will be the highest
  558. in the tree in order you have the shortest encoding and the less frequent bytes
  559. will be the lowest in the tree.
  560.  
  561. From an algorithmic point of view, you make a list of each byte you encountered
  562. in the stream to compress. This list will always be sorted. The zero-occurrence
  563. bytes are removed from this list. You take the two bytes with the smallest
  564. occurrences in the list. Whenever two bytes have the same "weight", you take two
  565. of them regardless to their ASCII value. You join them in a node. This node will
  566. have a fictive byte value (256 will be a good one!) and its weight will be
  567. the sum of the two joined bytes. You replace then the two joined bytes with
  568. the fictive byte. And you continue so until you have one byte (fictive or not)
  569. in the list. Of course, this process will produce the shortest codes if the list
  570. remains sorted. I will not explain with arcana hard maths why the result
  571. is a set of the shortest bytes...
  572.  
  573. Important: I use as convention that the right sub-trees have a weight greater
  574. or equal to the weight of the left sub-trees.
  575.  
  576. Example: Let's take a file to compress where we notice the following
  577. occurrences:
  578.  
  579. Listed bytes | Frequences (Weight)
  580. ----------------------------------
  581.       0      |        338
  582.      255     |        300
  583.       31     |        280
  584.       77     |         24
  585.      115     |         21
  586.       83     |         20
  587.      222     |         5
  588.  
  589. We will begin by joining the bytes 83 and 222. This will produce a fictive node
  590. 1 with a weight of 20+5=25.
  591.  
  592. (Fictive 1,25)
  593.       /\
  594.      /  \
  595. (222,5) (83,20)
  596.  
  597. Listed bytes | Frequences (Weight)
  598. ----------------------------------
  599.       0      |        338
  600.      255     |        300
  601.       31     |        280
  602.   Fictive 1  |         25
  603.       77     |         24
  604.      115     |         21
  605.  
  606. Note that the list is sorted... The smallest values in the frequences are 21 and
  607. 24. That is why we will take the bytes 77 and 115 to build the fictive node 2.
  608.  
  609. (Fictive 2,45)
  610.       /\
  611.      /  \
  612. (115,21) (77,25)
  613.  
  614. Listed bytes | Frequences (Weight)
  615. ----------------------------------
  616.       0      |        338
  617.      255     |        300
  618.       31     |        280
  619.   Fictive 2  |         45
  620.   Fictive 1  |         25
  621.  
  622. The nodes with smallest weights are the fictive 1 and 2 nodes. These are joined
  623. to build the fictive node 3 whose weight is 40+25=70.
  624.  
  625.     (Fictive 3,70)
  626.          /   \
  627.        /       \
  628.      /           \
  629.        /\            / \
  630.      /   \          /    \
  631. (222,5)  (83,20) (115,21) (77,25)
  632.  
  633. Listed bytes | Frequences (Weight)
  634. ----------------------------------
  635.       0      |        338
  636.      255     |        300
  637.       31     |        280
  638.   Fictive 3  |         70
  639.  
  640. The fictive node 3 is linked to the byte 31. Total weight: 280+70=350.
  641.  
  642.          (Fictive 4,350)
  643.            /   \
  644.          /       \
  645.            /           \
  646.          /  \       (31,280)
  647.        /      \
  648.      /          \
  649.        /\            / \
  650.      /   \          /    \
  651. (222,5)  (83,20) (115,21) (77,25)
  652.  
  653. Listed bytes | Frequences (Weight)
  654. ----------------------------------
  655.   Fictive 4  |        350
  656.       0      |        338
  657.      255     |        300
  658.  
  659. As you see, being that we sort the list, the fictive node 4 has become the first
  660. of the list. We join the bytes 0 and 255 in a same fictive node, the number 5
  661. whose weight is 338+300=638.
  662.  
  663. (Fictive 5,638)
  664.     /\
  665.        /  \
  666. (255,300) (0,338)
  667.  
  668. Listed bytes | Frequences (Weight)
  669. ----------------------------------
  670.   Fictive 5  |        638
  671.   Fictive 4  |        350
  672.  
  673. The fictive nodes 4 and 5 are finally joined. Final weight: 638+350=998 bytes.
  674. It is actually the total byte number in the initial file: 338+300+24+21+20+5.
  675.  
  676.              (Tree,998)
  677.                1     /  \     0
  678.               <-   /      \   ->
  679.              /          \
  680.                /              \
  681.              /                  \
  682.            /   \                / \
  683.          /       \             /    \
  684.            /           \          /       \
  685.          /  \       (31,280)  (255,300) (0,338)
  686.        /      \
  687.      /          \
  688.        /\            / \
  689.      /   \          /    \
  690. (222,5)  (83,20) (115,21) (77,25)
  691.  
  692. Bytes | Huffman codes | Frequences | Binary length*Frequence
  693. ------------------------------------------------------------
  694.   0   |       00b     |     338    |           676
  695.  255  |       01b     |     300    |           600
  696.   31  |       10b     |     280    |           560
  697.   77  |      1101b    |      24    |            96
  698.  115  |      1100b    |      21    |            84
  699.   83  |      1110b    |      20    |            80
  700.  222  |      1111b    |      5     |            20
  701.  
  702. Results: Original file size: (338+300+280+24+21+20+5)*8=7904 bits (=998 bytes)
  703. versus 676+600+560+96+84+80+20=2116 bits, i.e. 2116/8=265 bytes.
  704.  
  705. Now you know how to code an input stream. The last problem is to decode all this
  706. stuff. Actually, when you meet a binary sequence you can't say whether it comes
  707. from such byte list or such other one. Furthermore, if you change the occurrence
  708. of one or two bytes, you won't obtain the same resulting binary tree. Try for
  709. example to encode the previous list but with the following occurrences:
  710.  
  711. Listed bytes | Frequences (Weight)
  712. ----------------------------------
  713.      255     |        418
  714.       0      |        300
  715.       31     |        100
  716.       77     |         24
  717.      115     |         21
  718.       83     |         20
  719.      222     |         5
  720.  
  721. As you can observe it, the resulting binary tree is quite different, we had yet
  722. the same initial bytes. To not be in such a situation we will put an header
  723. in front of all data. I can't comment longly this header but I can say
  724. I minimize it as much as I could. The header is divided into two parts.
  725. The first part of this header looks closely to a boolean table (coded more or
  726. less in binary to save space) and the second part provide to the decoder
  727. the binary code associated to each byte encountered in the original input
  728. stream.
  729.  
  730. Here is a summary of the header:
  731.  
  732. First part
  733. ----------
  734.             First bit
  735.               /  \
  736.               1 /      \ 0
  737.               /          \
  738.   256 bits set to 0 or 1        5 bits for the number n (minus 1)
  739.   depending whether the         of bytes encountered
  740.   corresponding byte was        in the file to compres
  741.   in the file to compress                   |
  742.   (=> n bits set to 1,                     \ /
  743.    n>32)                        n values of 8-bits (n<=32)
  744.              \           /
  745.                \       /
  746.              \   /
  747. Second part                |
  748. -----------                |
  749.                |
  750.         +------------->|
  751. (n+1) times |              |
  752. (n bytes of |          First bit?
  753. the values  |            /   \
  754. encountered |         1 /      \ 0
  755. in the      |          /        \ 
  756. source file |   8 bits of      5 bits of the 
  757. + the code  | the length       length (-1)
  758. of a        | (-1) of the      of the following
  759. fictive     | following        binary
  760. byte        | binary code      code
  761. to stop the | (length>32)      (length<=32)
  762. decoding.   |          \       /
  763. The fictive |           \     /
  764. is set to   |            \   /
  765. 256 in the  |              |
  766. Huffman     |         binary code
  767. -tree of    |              |
  768. encoding)   +--------------|
  769.                |
  770.         Binary encoding of the source file
  771.                |
  772.           Code of end of encoding
  773.                |
  774.  
  775.  
  776. With my codecs I can handle binary sequences with a length of 256 bits.
  777. This correspond to encode all the input stream from one byte to infinite length.
  778. In fact if a byte had a range from 0 to 257 instead of 0 to 255, I would have a
  779. bug with my codecs with an input stream of at least 370,959,230,771,131,880,927,
  780. 453,318,055,001,997,489,772,178,180,790,105 bytes !!!
  781.  
  782. Where come this explosive number? In fact, to have a severe bug, I must have
  783. a completely unbalanced tree:
  784.  
  785.    Tree
  786.     /\
  787.       \
  788.       /\
  789.     \
  790.     /\
  791.       \
  792.       ...
  793.        /\
  794.          \
  795.          /\
  796.  
  797. Let's take the following example:
  798.  
  799. Listed bytes | Frequences (Weight)
  800. ----------------------------------
  801.       32     |         5
  802.       101    |         3
  803.       97     |         2
  804.       100    |         1
  805.       115    |         1
  806.  
  807. This produces the following unbalanced tree:
  808.  
  809.     Tree
  810.      /\
  811. (32,5) \
  812.        /\
  813.  (101,3) \
  814.      /\
  815.    (97,2)  \
  816.        /\
  817.     (115,1)  (100,1)
  818.  
  819. Let's speak about a mathematical series: The Fibonacci series. It is defined as
  820. following:
  821.  
  822. { Fib(0)=0
  823. { Fib(1)=1
  824. { Fib(n)=Fib(n-2)+Fib(n-1)
  825.  
  826. Fib(0)=0, Fib(1)=1, Fib(2)=1, Fib(3)=2, Fib(4)=3, Fib(5)=5, Fib(6)=8, Fib(7)=13,
  827. etc.
  828.  
  829. But 1, 1, 2, 3, 5, 8 are the occurrences of our list! We can actually
  830. demonstrate that to have an unbalanced tree, we have to take a list with
  831. an occurrence based on the Fibonacci series (these values are minimal).
  832. If the data to compress have m different bytes, when the tree is unbalanced,
  833. the longest code need m-1 bits. In our little previous example where m=5,
  834. the longest codes are associated to the bytes 100 and 115, respectively coded
  835. 0001b and 0000b. We can also say that to have an unbalanced tree we must have
  836. at least 5+3+2+1+1=12=Fib(7)-1. To conclude about all that, with a coder that
  837. uses m-1 bits, you must never have an input stream size over than Fib(m+2)-1,
  838. otherwise, there could be a bug in the output stream. Of course, with my codecs
  839. there will never be a bug because I can deal with binary code sizes of 1 to 256
  840. bits. Some encoder could use that with m=31, Fib(31+2)-1=3,524,577 and m=32,
  841. Fib(32+2)-1=5,702,886. And an encoder that uses unisgned integer of 32 bits
  842. shouldn't have a bug until about 4 Gb...
  843.  
  844. +==========================================================+
  845. |                     The LZW encoding                     |
  846. +==========================================================+
  847.  
  848. The LZW scheme is due to three searchers, i.e. Abraham Lempel and Jacob Ziv
  849. worked on it in 1977, and Terry Welch achieved this scheme in 1984.
  850.  
  851. LZW is patented in USA. This patent, number 4,558,302, is covered by Unisys
  852. Corporation and CompuServe. IBM seems to have discovered the same, and patented
  853. it. (Who's right???)
  854. You may get a limited licence by writting to:
  855. Welch Licencing Department
  856. Office of the General Counsel
  857. M/S C1SW19
  858. Unisys corporation
  859. Blue Bell
  860. Pennsylvania, 19424 (USA)
  861.  
  862. If you're occidental, you are surely using an LZW encoding every time you are
  863. speaking, especially when you use a dictionary. Let's consider, for example,
  864. the word "Cirrus". As you read a dictionary, you begin with "A", "Aa", and so
  865. on. But a computer has no experience and it must suppose that some words
  866. already exist. That is why with "Cirrus", it supposes that "C", "Ci", "Cir",
  867. "Cirr", "Cirru", and "Cirrus" exist. Of course, being that this is a computer,
  868. all these words are encoded as index numbers. Every time you go forward, you add
  869. a new number associated to the new word. Being that a computer is byte-based
  870. and not alphabetic-based, you have an initial dictionary of 256 letters instead
  871. of our 26 ('A' to 'Z') letters.
  872.  
  873. Example: Let's code "XYXYZ". First step, "X" is recognized in the initial
  874. dictionary of 256 letters as the 89th. Second step, "Y" is read. Does "XY"
  875. exist? No, then "XY" is stored as the word 256. You write in the output stream
  876. the ASCII of "X", i.e. 88. Now "YX" is tested as not referenced in the current
  877. dictionary. It is stored as the word 257. You write now in the output stream 89
  878. (ASCII of "Y"). "XY" is now met. But now "XY" is known as the reference 256.
  879. Being that "XY" exists, you test the sequence with one more letter, i.e. "XYZ".
  880. This last word is not referenced in the current dictionary. You write then the
  881. value 256. Finally, you reach the last letter ("Z"). You add "YZ" as the
  882. reference 258 but it is the last letter. That is why you just write the value
  883. 90 (ASCII of "Z").
  884.  
  885. Another encoding sample with the string "ABADABCCCABCEABCECCA".
  886.  
  887. +----+-----+---------------+------+----------+-------------------------+------+
  888. |Step|Input|Dictionary test|Prefix|New symbol|Dictionary               |Output|
  889. |    |     |               |      |          |D0=ASCII with 256 letters|      |
  890. +----+-----+---------------+------+----------+-------------------------+------+
  891. |  1 | "A" |"A" in D0      | "A"  |    "B"   | D1=D0                   |  65  |
  892. |    | "B" |"AB" not in D0 |      |          | and "AB"=256            |      |
  893. +----+-----+---------------+------+----------+-------------------------+------+
  894. |  2 | "A" |"B" in D1      | "B"  |    "A"   | D2=D1                   |  66  |
  895. |    |     |"BA" not in D1 |      |          | and "BA"=257            |      |
  896. +----+-----+---------------+------+----------+-------------------------+------+
  897. |  3 | "D" |"A" in D2      | "A"  |    "D"   | D3=D2                   |  65  |
  898. |    |     |"AD" not in D2 |      |          | and "AD"=258            |      |
  899. +----+-----+---------------+------+----------+-------------------------+------+
  900. |  4 | "A" |"D" in D3      | "D"  |    "A"   | D4=D3                   |  68  |
  901. |    |     |"DA" not in D3 |      |          | and "DA"=259            |      |
  902. +----+-----+---------------+------+----------+-------------------------+------+
  903. |  5 | "B" |"A" in D4      | "AB" |    "C"   | D5=D4                   |  256 |
  904. |    | "C" |"AB" in D4     |      |          | and "ABC"=260           |      |
  905. |    |     |"ABC" not in D4|      |          |                         |      |
  906. +----+-----+---------------+------+----------+-------------------------+------+
  907. |  6 | "C" |"C" in D5      | "C"  |    "C"   | D6=D5                   |  67  |
  908. |    |     |"CC" not in D5 |      |          | and "CC"=261            |      |
  909. +----+-----+---------------+------+----------+-------------------------+------+
  910. |  7 | "C" |"C" in D6      | "CC" |    "A"   | D7=D6                   |  261 |
  911. |    | "A" |"CC" in D6     |      |          | and "CCA"=262           |      |
  912. |    |     |"CCA" not in D6|      |          |                         |      |
  913. +----+-----+---------------+------+----------+-------------------------+------+
  914. |  8 | "B" |"A" in D7      | "ABC"|    "E"   | D8=D7                   |  260 |
  915. |    | "C" |"AB" in D7     |      |          | and "ABCE"=263          |      |
  916. |    | "E" |"ABC" in D7    |      |          |                         |      |
  917. |    |    <"ABCE" not in D7|      |          |                         |      |
  918. +----+-----+---------------+------+----------+-------------------------+------+
  919. |  9 | "A" |"E" in D8      | "E"  |    "A"   | D9=D8                   |  69  |
  920. |    |     |"EA" not in D8 |      |          | and "EA"=264            |      |
  921. +----+-----+---------------+------+----------+-------------------------+------+
  922. | 10 | "B" |"A" in D9      |"ABCE"|    "C"   | D10=D9                  |  263 |
  923. |    | "C" |"AB" in D9     |      |          | and "ABCEC"=265         |      |
  924. |    | "E" |"ABC" in D9    |      |          |                         |      |
  925. |    | "C" |"ABCE" in D9   |      |          |                         |      |
  926. |    |    <"ABCEC" not in D9>     |          |                         |      |
  927. +----+-----+---------------+------+----------+-------------------------+------+
  928. | 11 | "C" |"C" in D10     | "CCA"|          |                         |  262 |
  929. |    | "A" |"CC" in D10    |      |          |                         |      |
  930. |    |    <"CCA" not in D10|      |          |                         |      |
  931. +----+-----+---------------+------+----------+-------------------------+------+
  932.  
  933. You will notice a problem with the above output: How to write a code of 256
  934. (for example) on 8 bits? It's simple to solve this problem. You just say that
  935. the encoding starts with 9 bits and as you reach the 512th word, you use a
  936. 10-bits encoding. With 1024 words, you use 11 bits; with 2048 words, 12 bits;
  937. and so on with all numbers of 2^n (n is positive). To better synchronize
  938. the coder and the decoder with all that, most of implementations use two
  939. additional references. The word 256 is a code of reinitialisation (the codec
  940. must reinitialize completely the current dictionary to its 256 initial letters)
  941. and the word 257 is a code of end of information (no more data to read).
  942. Of course, you start your first new word as the code number 258.
  943.  
  944. You can also do so as in the GIF file format and start with an initial
  945. dictionary of 18 words to code an input stream with only letters coded on 4 bits
  946. (you start with codes of 5 bits in the output stream!). The 18 initial words
  947. are: 0 to 15 (initial letters), 16 (reinit the dictionary), and 17 (end of
  948. information). First new word has code 18, second word, code 19, ...
  949.  
  950. Important: You can consider that your dictionary is limited to 4096 different
  951. words (as in GIF and TIFF file formats). But if your dictionary is full, you
  952. can decide to send old codes *without* reinitializing the dictionary. All the
  953. decoders must be compliant with this. This enables you to consider that it is
  954. not efficient to reinitialize the full dictionary. Instead of this, you don't
  955. change the dictionary and you send/receive (depending if it's a coder or a
  956. decoder) existing codes in the full dictionary.
  957.  
  958. My codecs are able to deal as well with most of initial size of data in the
  959. initial dictionary as with full dictionary.
  960.  
  961. Let's see how to decode an LZW encoding. We saw with true dynamical Huffman
  962. scheme that you needed an header in the encoding codes. Any header is useless
  963. in LZW scheme. When two successive bytes are read, the first must exist in the
  964. dictionary. This code can be immediately decoded and written in the output
  965. stream. If the second code is equal or less than the word number in the current
  966. dictionary, this code is decoded as the first one. At the opposite, if the
  967. second code is equal to the word number in dictionary plus one, this means you
  968. have to write a word composed with the word (the sentence, not the code number)
  969. of the last code plus the first character of the last code. In between, you make
  970. appear a new word. This new word is the one you just sent to the output stream,
  971. it means composed by all the letters of the word associated to the first code
  972. and the first letter of the word of the second code. You continue the processing
  973. with the second and third codes read in the input stream (of codes)...
  974.  
  975. Example: Let's decode the previous encoding given a bit more above.
  976.  
  977. +------+-------+----------------+----------+------------------+--------+
  978. | Step | Input | Code to decode | New code |    Dictionary    | Output |
  979. +------+-------+----------------+----------+------------------+--------+
  980. |   1  |   65  |       65       |    66    |     65,66=256    |   "A"  |
  981. |      |   66  |                |          |                  |        |
  982. +------+-------+----------------+----------+------------------+--------+
  983. |   2  |   65  |       66       |    65    |     66,65=257    |   "B"  |
  984. +------+-------+----------------+----------+------------------+--------+
  985. |   3  |   68  |       65       |    68    |     65,68=258    |   "A"  |
  986. +------+-------+----------------+----------+------------------+--------+
  987. |   4  |  256  |       68       |    256   |     68,65=259    |   "D"  |
  988. +------+-------+----------------+----------+------------------+--------+
  989. |   5  |   67  |       256      |    67    |   65,66,67=260   |   "AB" |
  990. +------+-------+----------------+----------+------------------+--------+
  991. |   6  |  261  |       67       |    261   |     67,67=261    |   "C"  |
  992. +------+-------+----------------+----------+------------------+--------+
  993. |   7  |  260  |       261      |    260   |   67,67,65=262   |   "CC" |
  994. +------+-------+----------------+----------+------------------+--------+
  995. |   8  |   69  |       260      |    69    |  65,66,67,69=263 |  "ABC" |
  996. +------+-------+----------------+----------+------------------+--------+
  997. |   9  |  263  |       69       |    263   |     69,65=264    |   "E"  |
  998. +------+-------+----------------+----------+------------------+--------+
  999. |  10  |  262  |       263      |    262   |65,66,67,69,67=256| "ABCE" |
  1000. +------+-------+----------------+----------+------------------+--------+
  1001. |  11  |       |       262      |          |                  |  "CCA" |
  1002. +------+-------+----------------+----------+------------------+--------+
  1003.  
  1004. Summary: The step 4 is an explicit example. The code to decode is 68 ("D" in
  1005. ASCII) and the new code is 256. The new word to add to the dictionary is the
  1006. letters of the first word plus the the first letter of the second code (code
  1007. 256), i.e. 65 ("A" in ASCII) plus 68 ("D"). So the new word has the letters 68
  1008. and 65 ("AD").
  1009.  
  1010. The step 6 is quite special. The first code to decode is referenced but the
  1011. second new code is not referenced being that the dictionary is limited to 260
  1012. referenced words. We have to make it as the second previously given case, it
  1013. means you must take the word to decode plus its first letter, i.e. "C"+"C"="CC".
  1014. Be care, if any encountered code is *upper* than the dictionary size plus 1, it
  1015. means you have a problem in your data and/or your codecs are...bad!
  1016.  
  1017. Tricks to improve LZW encoding (but it becomes a non-standard encoding):
  1018. - To limit the dictionary to an high amount of words (4096 words maximum enable
  1019. you to encode a stream of a maximmum 7,370,880 letters with the same dictionary)
  1020. - To use a dictionary of less than 258 if possible (example, with 16 color
  1021. pictures, you start with a dictionary of 18 words)
  1022. - To not reinitialize a dictionary when it is full
  1023. - To reinitialize a dictionary with the most frequent of the previous dictionary
  1024. - To use the codes from (current dictionary size+1) to (maximum dictionary size)
  1025. because these codes are not used in the standard LZW scheme.
  1026. Such a compression scheme has been used (successfully) by Robin Watts
  1027. <ct93008@ox.ac.uk>.
  1028.  
  1029. +==========================================================+
  1030. |                         Summary                          |
  1031. +==========================================================+
  1032.  
  1033. -------------------------------------------------
  1034. RLE type 1:
  1035. Fastest compression. Good ratio for general purpose.
  1036. Doesn't need to read the data by twice.
  1037. Decoding fast.
  1038. -------------------------------------------------
  1039. RLE type 2:
  1040. Fast compression. Very good ratio in general (even for general purposes).
  1041. Need to read the data by twice.
  1042. Decoding fast.
  1043. -------------------------------------------------
  1044. RLE type 3:
  1045. Slowest compression. Good ratio on image file,quite middle for general purposes.
  1046. Need to read the data by twice.
  1047. Change line:
  1048. #define MAX_RASTER_SIZE 256
  1049. into:
  1050. #define MAX_RASTER_SIZE 16
  1051. to speed up the encoding (but the result decreases in ratio). If you compress
  1052. with memory buffers, do not modify this line...
  1053. Decoding fast.
  1054. -------------------------------------------------
  1055. RLE type 4:
  1056. Slow compression. Good ratio on image file, middle in general purposes.
  1057. Change line:
  1058. #define MAX_RASTER_SIZE 66
  1059. into:
  1060. #define MAX_RASTER_SIZE 16
  1061. to speed up the encoding (but the result decreases in ratio). If you compress
  1062. with memory buffers, do not modify this line...
  1063. Decoding fast.
  1064. -------------------------------------------------
  1065. Huffman:
  1066. Fast compression. Good ratio on text files and similar, middle for general
  1067. purposes. Interesting method to use to compress a buffer already compressed by
  1068. RLE types 1 or 2 methods...
  1069. Decoding fast.
  1070. -------------------------------------------------
  1071. LZW:
  1072. Quite fast compression. Good, see even very good ratio, for general purposes.
  1073. Bigger the data are, better the compression ratio is.
  1074. Decoding quite fast.
  1075. -------------------------------------------------
  1076.  
  1077. The source codes work on all kinds of computers with a C compiler.
  1078. With the compiler, optimize the speed run option instead of space option.
  1079. With UNIX system, it's better to compile them with option -O.
  1080. If you don't use a GNU compiler, the source file MUST NOT have a size
  1081. over 4 Gb for RLE 2, 3, and Huffman, because I count the number
  1082. of occurrences of the bytes.
  1083. So, with GNU compilers, 'unsigned lont int' is 8 bytes instead of 4 bytes
  1084. (as normal C UNIX compilers and PCs' compilers, such as Microsoft C++
  1085. and Borland C++).
  1086. Actually:
  1087. * Normal UNIX compilers,                => 4 Gb (unsigned long int = 4 bytes)
  1088.   Microsoft C++ and Borland C++ for PCs
  1089. * GNU UNIX compilers                    => 17179869184 Gb (unsigned long int = 8 bytes)
  1090.  
  1091. +==========================================================+
  1092. |                             END                          |
  1093. +==========================================================+
  1094.